package fr.mywiki.business.entry;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.user.User;
import fr.mywiki.business.utils.StringUtils;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.business.versioning.VersionManager;
import fr.mywiki.model.ejbAdapter.ServicesFacade;
import fr.mywiki.model.ejbAdapter.utils.PersistenceException;


/**
 * Contains several methods useful when creating an entry object.
 * 
 * @author tlombard
 */
public class EntryManager {
	private EntryManager() {}

	/**
	 * Returns the intern form of the title.
	 * 
	 * @param realTitle
	 *            the title to be displayed in the entry page.
	 * @return a <code>String</code> object representing the intern title of
	 *         the entry.
	 */
	public static String getInternTitle(String realTitle) {
		return StringUtils.getInternForm(realTitle);
	}

	/**
   * Updates the given entry with the values given in parameters (and other
   * computed). First, the previous version of the entry is archived. Then, the
   * rights are updated.
   * 
   * @param entry
   *          a <code>EntryLite</code> object, the entry to modify.
   * @param title
   *          the title of the entry.
   * @param content
   *          the properties of the entry.
   * @param readsStructs
   *          a <code>Collection</code> of <code>StructureLite</code>
   *          objects.
   * @param editStructs
   *          a <code>Collection</code> of <code>StructureLite</code>
   *          objects.
   * @param request
   *          the current request (helps to find the author).
   */
	public static void updateEntry(EntryLite entry, Map content, Collection readStructs, Collection editStructs, 
			HttpServletRequest request) throws RemoteException, PersistenceException, FinderException, SQLException {
		VersionManager.getInstance().addVersion(entry, content, new User(request));
    
    // Manage rights
    EntryAccessManager.updateEntryRights(entry, readStructs, editStructs);
	}
	
	/**
   * Creates a new entry with the values given in parameter. The read and edit
   * rights are also given in parameter.
   * 
   * @param title
   *          the title of the entry.
   * @param content
   *          the properties of the entry.
   * @param request
   *          the current request (helps to find the author)
   * @param readsStructs
   *          a <code>Collection</code> of <code>StructureLite</code>
   *          objects.
   * @param editStructs
   *          a <code>Collection</code> of <code>StructureLite</code>
   *          objects.
   * @return a <code>EntryLite</code> object, the created entry.
   */
  public static EntryLite createEntry(String title, Map content,
      Collection readStructs, Collection editStructs, HttpServletRequest request)
      throws RemoteException, PersistenceException, FinderException, SQLException {
    EntryLite entry = (EntryLite) ServicesFacade.getInstance().create(
        EntryLite.TYPE_ENT);

    entry.setTitle(title);
    entry.setInternTitle(getInternTitle(title));

    // Add this version to the set of versions linked to the entry
    VersionLite version = VersionManager.getInstance().addVersion(entry,
        content, new User(request));
    entry.addLink(EntryLite.LINK_VERSION_ENTRY, version);

    // The latest version is the current version
    entry.setCurrentVersion(version.getId());

    entry.update();
    
    // Manage the rights of this new entry
    EntryAccessManager.updateEntryRights(entry, readStructs, editStructs);

    return entry;
  }
  
  /**
   * Returns the current version of the entry given in parameter.
   * 
   * @param entry an <code>EntryLite</code> object.
   * @return a <code>VersionLite</code> object.
   */
  public static VersionLite getCurrentVersion(EntryLite entry) throws RemoteException, FinderException {
    return VersionManager.getInstance().getCurrentVersion(entry);
  }
  
  /**
	 * Returns the current content of an entry. It is not wiki-formatted.
	 * 
	 * @param entry a <code>EntryLite</code> object
	 * @return a String
	 */
	public static String getCurrentContent(EntryLite entry)
			throws RemoteException, FinderException {
		return VersionManager.getInstance().getVersionContent(
				getCurrentVersion(entry));
	}
	
	/**
	 * Deletes the entry given in parameter. The versions of this article are also deleted.
	 * 
	 * @param entry a <code>EntryLite</code> object.
	 * @throws RemoteException 
	 * @throws ObjectNotFoundException 
	 */
	public static void deleteEntry(EntryLite entry) throws ObjectNotFoundException, RemoteException {
		List versions = VersionManager.getInstance().getVersions(entry);
		
		for (int i = versions.size() - 1; i >= 0; i--) {
			VersionLite version = (VersionLite) versions.get(i);
			version.remove();
		}
		
		entry.remove();
	}
}